package ugc.data;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import library.database.MyQueryRunner;

import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Test;

import crawler.web.engine.config.SpBN;
import crawler.web.engine.config.SpringUtils;

/**
 * @author waynechen May 14, 2012 2:34:50 PM
 */
public class DataCopyMainSite {

	private MyQueryRunner main;
	private MyQueryRunner ugc;

	@Before
	public void init() {
		main = SpringUtils.getBean(SpBN.bean_mainsite_query_runner);
		ugc = SpringUtils.getBean(SpBN.bean_ugc_query_runner);
	}

	@Test
	public void copyDesc() throws IOException, SQLException {
		File f = new File("/home/waynechen/Desktop/update.sql");
		if (f.exists())
			f.delete();
		BufferedWriter writer = new BufferedWriter(new FileWriter(f));
		List<Map<String, Object>> maps = main.query("select name,introduction from Sight", new MapListHandler());
		for (Map<String, Object> map : maps) {
			try {
				String name = (String) map.get("name");
				String desc = (String) map.get("introduction");
				name = name.trim();
				if (StringUtils.isEmpty(desc))
					continue;
				else {
					desc = desc.replace("\n", "").replace("\r", "");
					desc = desc.replace("'", "\"").replace("--", "~~");
				}
				String sql = String.format("update address set description='%s' where name='%s';\n", desc, name);
				//				ugc.update("update address set description=? where name=?", desc, name);
				writer.append(sql);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
		writer.flush();
		writer.close();
	}
}

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//